// pages/saleroom/saleroom.js
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      currentPage: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this;
      that.system();
      that.storemanageList(that.data.currentPage);
      that.groupgoodsDetail();
  },
    system:function(e){
        var that=this;
        utils.http("/api/v1/common/system", {
            store_id: wx.getStorageSync("userType").storeid,
        }, function (res) {
            that.setData({
                sys: res.datas,
            })
        })
    },
    groupgoodsDetail: function (e) {
        var that = this;
        utils.http("/api/v1/Storemanage/store_info", {
            store_id: wx.getStorageSync("userType").storeid,
        }, function (res) {
            that.setData({
                data: res.datas,
            })
        })
    },
    storemanageList: function (currentPage){
        var that=this;
        utils.showLoading();
        utils.http("/api/v1/storemanage/withdrawal_log", {
            store_id: wx.getStorageSync("userType").storeid,
            page:currentPage
        }, function (res) {
            utils.hideLoading();
            var listpost=that.data.list||[];
            for(var i in res.datas){
                // console.log(res.datas[i].create_time*1000)
                // var dataTime = utils.getPastTime(res.datas[i].create_time * 1000)
                // console.log(dataTime)
                // console.log(res.datas[i].create_time)
                // console.log(utils.formatTime(1488481383))
               // res.datas[i].dataTime = utils.formatTime(res.datas[i].create_time)
                listpost.push(res.datas[i]);
            }
            if (res.datas.length < 10) {
                that.setData({
                    tishi_title: '已加载全部',
                });
            } else {
                that.setData({
                    tishi_title: '加载更多',
                });
            }
            that.setData({
                list: listpost,
                ready: res.datas.length == 0 ? 1 : 2,
            })
            console.log(that.data.list)
        })
    },
    //提现到微信
    onWithdrawalTap:function(e){
        var that=this;
        console.log(e.currentTarget.dataset.available)
        console.log(e.currentTarget.dataset.money)
        if (e.currentTarget.dataset.available < e.currentTarget.dataset.money) {
            wx.showToast({
                title: '单笔满' + e.currentTarget.dataset.money + '元可提现',
                icon:"none"
            })
            return false
        }
            utils.http("/api/v1/storemanage/withdrawal", {
                store_id: wx.getStorageSync("userType").storeid,
                amount: e.currentTarget.dataset.available
            }, function (res) {
                console.log(res)

                if (res.state == 1) {
                    that.setData({
                        currentPage: 1,
                        list: []
                    })
                    wx.showToast({
                        title: '提现成功',
                    })
                    setTimeout(function(){
                        that.groupgoodsDetail();
                        that.storemanageList(that.data.currentPage);
                    },800)
                    
                }
            })
        
        
    },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.currentPage + 1
            that.setData({
                currentPage: page,
            })
            that.storemanageList(page)
        }

    },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})